import ConfirmPopup from "./components/popups/ConfirmPopup"
import BaseScene from "./core/BaseScene";
const { ccclass, property } = cc._decorator;

//射箭游戏

@ccclass
export default class archery extends BaseScene {

    @property(cc.Prefab)
    popBox: cc.Prefab = null;

    @property(cc.Node)
    roundNode: cc.Node = null;

    @property(cc.Node)
    actNode: cc.Node = null;

    @property(cc.SpriteFrame)
    arrow1: cc.SpriteFrame = null;

    @property(cc.SpriteFrame)
    arrow2: cc.SpriteFrame = null;

    @property(cc.SpriteFrame)
    bg: cc.SpriteFrame = null;

    angel: number = 0;
    arrowSpeed: number = 5000; //箭得速度
    runDistance: number = 320; //从发射到转盘的距离
    shotCount: number = 0;     //总共成功插入箭的个数
    arrowArr: number[] = [];
    gameState: number = 0; // 0未开始，1开始，2结束
    popCtrl: ConfirmPopup = null;

    onLoad(): void {
        super.onLoad();
    }

    start() {
        this.startGame();
    }

    startGame() {
        this.angel = 0
        this.shotCount = 0;     //总共成功插入箭的个数
        this.arrowArr = [];
        this.roundNode.removeAllChildren()
        this.roundNode.angle = 0
        let node = new cc.Node("New Sprite");
        let sprite = node.addComponent(cc.Sprite);
        sprite.spriteFrame = this.bg
        node.zIndex = 999;
        this.roundNode.addChild(node);

        for (let i = 0; i < 3; i++) {
            this.arrowArr.push(this.angel);
            this.roundNode.angle = -this.angel;
            this.angel += 90;
            let sprArrow = this.createSprite();
            sprArrow.angle = -this.roundNode.angle;
            this.roundNode.addChild(sprArrow);
        }
        this.gameState = 1;
    }

    createSprite(isGameOver: boolean = false) {
        let node = new cc.Node("New Sprite");
        node.setAnchorPoint(0.5, 1);
        let sprite = node.addComponent(cc.Sprite);
        sprite.spriteFrame = isGameOver ? this.arrow2 : this.arrow1;
        return node;
    }

    update() {
        if (this.gameState == 1)
            this.doRotate();
    }

    doRotate() {
        let curAngel = -this.roundNode.angle;
        if (curAngel <= this.angel) {
            this.roundPanel();
        }
    }

    roundPanel() {
        if (this.shotCount < 6)
            this.angel += 1.5;
        else if (this.shotCount < 11)
            this.angel += 2.0;
        else if (this.shotCount < 16)
            this.angel += 2.5;
        else
            this.angel += 3;
        this.roundNode.angle = -this.angel;
    }

    btnClick() {
        let runTime = this.runDistance / this.arrowSpeed;
        cc.tween(this.roundNode)
            .delay(runTime)
            .call(() => {
                let angle = this.roundNode.angle;
                let _angle = Math.abs(angle)
                if (_angle >= 360)
                    _angle = _angle % 360;
                this.arrowArr.push(_angle);
                //检查游戏是否结束
                let isGameOver = this.checkGameOver();
                let sprArrow = this.createSprite(isGameOver);
                sprArrow.angle = -angle;
                this.roundNode.addChild(sprArrow);
                if (isGameOver) {
                    this.gameState = 2;
                    this.showPopup()
                } else {
                    this.shotCount++;
                }
            })
            .start();
    }

    checkGameOver() {
        this.arrowArr.sort((a, b) => a - b);
        for (let i = 0; i < this.arrowArr.length - 1; i++) {
            let arrow1 = this.arrowArr[i];
            let arrow2 = this.arrowArr[i + 1];
            if (arrow2 - arrow1 < 8 || (arrow2 > 352 && arrow2 < 360))
                return true;

        }
        return false;
    }

    showPopup() {
        if (this.popCtrl) {
            this.popCtrl.show({
                title: '提示',
                content: '游戏结束，得分' + this.shotCount,
                confirmCallback: () => {
                    this.startGame();
                }
            });
        } else {
            let node = cc.instantiate(this.popBox);
            node.parent = this.node;
            this.popCtrl = node.getComponent(ConfirmPopup)
            this.popCtrl.show({
                title: '提示',
                content: '游戏结束，得分' + this.shotCount,
                confirmCallback: () => {
                    this.startGame();
                }
            });
        }
    }

}
